package com.example.administrator.basedemos.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.basedemos.R;
import com.example.administrator.basedemos.utils.StatusBarUtils;
import com.hwangjr.rxbus.RxBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 创建者     ZCL
 * 创建时间   2018/5/25 9:01
 * 描述	      ${TODO}
 * <p>
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */
public abstract class BaseZcActivity extends AppCompatActivity {

    public Toolbar          mToolbar;
    public  TextView         mToolbarTitle;
    private FrameLayout      mBaseRealContent;
    private Unbinder mUnbinder;
    public View mToolbarLy;
    public ImageView mSetting;
    public TextView mConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        StatusBarUtils.setWindowStatusBarColor(this, R.color.yellow);


        RxBus.get().register(this);
        mToolbar = findViewById(R.id.z_toolbar);
        mToolbarLy = findViewById(R.id.z_toolbar_ly);
        mSetting = findViewById(R.id.z_toolbar_setting);
        mConfirm = findViewById(R.id.z_toolbar_confirm);
        mToolbarTitle = findViewById(R.id.z_toolbar_title);
        mBaseRealContent = findViewById(R.id.real_content);

        initView();


        mToolbar.setNavigationIcon(R.mipmap.back_b);
        mToolbar.setTitle("");

        //设置toolbar替换默认actionbar
        setSupportActionBar(mToolbar);
        //将activity压入栈内

        initData();
        initListener();

    }


    private void initView() {
        View view = View.inflate(this, setLayoutId(), null);
        mBaseRealContent.addView(view);
        mUnbinder = ButterKnife.bind(this);
    }
    public void setLayoutMangerVertical(RecyclerView recyclerView) {
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }

    public void setLayoutMangerHorizontal(RecyclerView recyclerView) {
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
    }

    protected abstract int setLayoutId();

    protected abstract void initData();


    public void setToolBarTitle(CharSequence title) {
        if (mToolbarTitle != null) {
            mToolbarTitle.setText(title);
        } else {
            // getToolbar().setTitle(title);
            //setSupportActionBar(getToolbar());
        }
    }

    public Toolbar getToolbar() {
        return mToolbar;
    }

    public void setToolbarBackGroundColor(int color) {
        mToolbar.setBackgroundColor(this.getResources().getColor(color));
    }

    public void setToolbarNavigationIcon(int color) {
        mToolbar.setNavigationIcon(color);
    }

    public void setToolbarBackGroundDraw(int resId) {
        mToolbar.setBackgroundResource(resId);
    }




    public void initListener() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }


    public void skipActivity(Activity aty, Class<?> cls) {
        showActivity(aty, cls);
        finish();
    }

    public void skipActivity(Intent it) {
        showActivity(it);
        finish();
    }

    public void skipActivity(Activity aty, Class<?> cls, Bundle extras) {
        showActivity(aty, cls, extras);
        finish();
    }


    public void showActivity(Activity aty, Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(aty, cls);
        startActivity(intent);
    }


    public void showActivity(Intent it) {
        startActivity(it);
    }


    public void showActivity(Activity aty, Class<?> cls, Bundle extras) {
        Intent intent = new Intent();
        intent.putExtras(extras);
        intent.setClass(aty, cls);
        startActivity(intent);
    }


    public void showActivityForResult(Activity aty, Class<?> cls, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(aty, cls);
        startActivityForResult(intent, requestCode);
    }

    public void showActivityForResult(Intent i, int requestCode) {
        startActivityForResult(i, requestCode);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
        RxBus.get().unregister(this);
    }
}
